// pages/userlist/userlist.js
import {$await} from '../../utils/util'
import runtime from '../../utils/runtime'
import {imgUrlAvatar} from '../../config'
import {usetlistAPI} from '../../api/userlist/index'
const img_url = 'https://images.weserv.nl/?url='
Component({

  /**
   * 页面的初始数据
   */
   properties:{
    atShow:{
      type:Boolean,
      value:false
    },
    number:{
      type:[String,Number],
      value:''
    },
    period:{
      type:[String,Number],
      value:''
    },
   },
  data: {
    searchList:[],
    userList:{},
    page:1,
    searchValue:'',
    isSearch:false
  },
  observers:{
    'atShow'(n){
      if(n){
        this.getUser(1)
      }
    }
  },
  // 组件生命周期
  lifetimes: {
    // 在组件实例进入页面节点树时执行
    created() {
    },
    attached() {
    },
    detached() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  methods: {
    // 判断@的用户是不是自己
    isSelf(user){
      let userInfo = wx.getStorageSync('userInfo')
      if(userInfo.user_id == user.number){
        wx.showToast({
          icon:'none',
          title: '不能@自己',
          duration: 2000
        })
        return false
      }
      return true
    },
    hideList(){
      this.triggerEvent('showAtlist', false)
    },
    atThis({currentTarget:{dataset}}){
      let info = dataset.info
      if(this.isSelf(info)){
        this.triggerEvent('atMsg', info)
        this.triggerEvent('showAtlist', false)
      }
      // this.setData({atShow:false})
    },
    async getUser(page){
      let params = {
        number: this.data.number,
        period: this.data.period,
        page,
        pageSize:20
      }
      let [err,res] = await $await(usetlistAPI.atList({...params}))
      if(res){
        for (const key in res) {
          if(res[key].length>0){
            res[key] = res[key].map(item =>{
              return {
                ...item,
                avatar:img_url+imgUrlAvatar+item.avatar
              }
            })
          }
        }
        if(page == 1){
          this.setData({userList:res})
        }else{
          const {userList}  = this.data
          userList.all_user = [].concat(...userList.all_user,...res.all_user)
          userList.hosts = userList.hosts.concat(res.hosts)
          this.setData({userList,page})
        }
      }
    },
    bindSearch(e){
      this.search(e.detail.value,true)
    },
    async search(keyword,isSearch){
      let params = {
        number: this.data.number,
        keyword,
        page:1,
        pageSize:20
      }
      if(keyword == ''){
        this.setData({isSearch:false})
        return
      }
      let [err,res] = await $await(usetlistAPI.search({...params}))
      if(res){
        res = res.map(item =>{
          return {
            ...item,
            avatar:img_url+imgUrlAvatar+item.avatar
          }
        })
        this.setData({
          searchList:res,
          isSearch:isSearch == true && keyword!='' ? true  :false
        })
      }
    },
    nextPage(e){
      console.log(e)
      this.getUser(this.data.page+1)
    },
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getUser(this.data.page)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})